package com.ruoyi.central.controller;

import java.util.List;

import com.ruoyi.common.validation.group.InsertGroup;
import com.ruoyi.common.validation.group.UpdateGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.utils.bean.SelectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.central.domain.TopCentralControllerAlarmInfo;
import com.ruoyi.central.service.ITopCentralControllerAlarmInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.validation.group.UpdateBatchGroup;
import com.ruoyi.common.core.domain.StateVo;
import java.util.Collection;
/**
 * 报警信息Controller
 * 
 * @author ruoyi
 * @date 2022-02-25
 */
@Controller
@RequestMapping("/central/centralControllerAlarmInfo")
public class TopCentralControllerAlarmInfoController extends BaseController
{
    private String prefix = "central/centralControllerAlarmInfo";

    @Autowired
    private ITopCentralControllerAlarmInfoService topCentralControllerAlarmInfoService;

    @RequiresPermissions("central:centralControllerAlarmInfo:view")
    @GetMapping()
    public String centralControllerAlarmInfo()
    {
        return prefix + "/centralControllerAlarmInfo";
    }

    /**
     * 查询报警信息列表
     */
    @RequiresPermissions("central:centralControllerAlarmInfo:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(TopCentralControllerAlarmInfo topCentralControllerAlarmInfo)
    {
        startPage();
        List<TopCentralControllerAlarmInfo> list = topCentralControllerAlarmInfoService.selectList(topCentralControllerAlarmInfo);
        return getDataTable(list);
    }

    /**
     * 查询简单的报警信息列表 用于select选择器
     */
    @GetMapping("/simpleList")
    @ResponseBody
    public AjaxResult simpleList(TopCentralControllerAlarmInfo topCentralControllerAlarmInfo){

        Collection<TopCentralControllerAlarmInfo> topCentralControllerAlarmInfoList = topCentralControllerAlarmInfoService.selectAllMap().values();

        return AjaxResult.success(SelectUtils.buildSelectList(topCentralControllerAlarmInfoList,TopCentralControllerAlarmInfo::getId,TopCentralControllerAlarmInfo::getAlarmCode));
    }

    /**
     * 获取报警信息详细信息
     */
    @RequiresPermissions("central:centralControllerAlarmInfo:query")
    @GetMapping(value = "/{id}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(topCentralControllerAlarmInfoService.selectById(id));
    }

    /**
     * 获取自增排序值
     * @param topCentralControllerAlarmInfo 过滤条件
     * @return
     */
    @GetMapping("/getIncrementWeight")
    @ResponseBody
    public AjaxResult getIncrementWeight(TopCentralControllerAlarmInfo topCentralControllerAlarmInfo){
        int weigth = topCentralControllerAlarmInfoService.getMaxOfWeight(topCentralControllerAlarmInfo);
        return AjaxResult.success(++weigth);
    }

    /**
     * 下载报警信息导入模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TopCentralControllerAlarmInfo> util = new ExcelUtil<>(TopCentralControllerAlarmInfo.class);
        util.importTemplateExcel(response, "报警信息数据");
    }

    /**
     * 导入报警信息数据
     * @param file 报警信息数据文件
     * @param updateSupport 是否为更新
     */
    @Log(title = "报警信息", businessType = BusinessType.IMPORT)
    @RequiresPermissions("central:centralControllerAlarmInfo:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TopCentralControllerAlarmInfo> util = new ExcelUtil<>(TopCentralControllerAlarmInfo.class);
        List<TopCentralControllerAlarmInfo> topCentralControllerAlarmInfoList = util.importExcel(file.getInputStream());

        if(updateSupport){
            if(topCentralControllerAlarmInfoService.updateBatchById(topCentralControllerAlarmInfoList)){
                return AjaxResult.success("导入报警信息数据覆盖成功！");
            }
            return AjaxResult.error("导入报警信息数据覆盖失败！");
        }
        if(topCentralControllerAlarmInfoService.insertBatch(topCentralControllerAlarmInfoList)){
            return AjaxResult.success("导入报警信息数据新增成功！");
        }
        return AjaxResult.error("导入报警信息数据新增失败！");
    }

    /**
     * 导出报警信息列表
     */
    @RequiresPermissions("central:centralControllerAlarmInfo:export")
    @Log(title = "报警信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TopCentralControllerAlarmInfo topCentralControllerAlarmInfo)
    {
        List<TopCentralControllerAlarmInfo> list = topCentralControllerAlarmInfoService.selectList(topCentralControllerAlarmInfo);
        ExcelUtil<TopCentralControllerAlarmInfo> util = new ExcelUtil<TopCentralControllerAlarmInfo>(TopCentralControllerAlarmInfo.class);
        return util.exportExcel(list, "报警信息数据");
    }

    /**
     * 新增报警信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存报警信息
     */
    @RequiresPermissions("central:centralControllerAlarmInfo:add")
    @Log(title = "报警信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ResponseBody
    public AjaxResult addSave(@Validated(InsertGroup.class) @RequestBody TopCentralControllerAlarmInfo topCentralControllerAlarmInfo)
    {
        return toAjax(topCentralControllerAlarmInfoService.insert(topCentralControllerAlarmInfo));
    }

    /**
     * 修改报警信息
     */
    @RequiresPermissions("central:centralControllerAlarmInfo:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        TopCentralControllerAlarmInfo topCentralControllerAlarmInfo = topCentralControllerAlarmInfoService.selectById(id);
        mmap.put("topCentralControllerAlarmInfo", topCentralControllerAlarmInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存报警信息
     */
    @RequiresPermissions("central:centralControllerAlarmInfo:edit")
    @Log(title = "报警信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ResponseBody
    public AjaxResult editSave(@Validated(UpdateGroup.class) @RequestBody TopCentralControllerAlarmInfo topCentralControllerAlarmInfo)
    {
        return toAjax(topCentralControllerAlarmInfoService.updateById(topCentralControllerAlarmInfo));
    }

    /**
     * 修改报警信息 状态 根据 ids 主键
     */
    @RequiresPermissions("central:centralControllerAlarmInfo:edit")
    @Log(title = "报警信息", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStateByIds")
    @ResponseBody
    public AjaxResult updateStateByIds(@Validated(value= UpdateBatchGroup.class)@RequestBody StateVo<Integer> stateVo)
    {
        return toAjax(topCentralControllerAlarmInfoService.updateStateByIds(stateVo.getIds(),stateVo.getState()));
    }

    /**
     * 恢复报警信息 为未删除 根据 ids 主键
     */
    @RequiresPermissions("central:centralControllerAlarmInfo:restore")
    @Log(title = "报警信息", businessType = BusinessType.UPDATE)
    @PutMapping("/restoreData/{ids}")
    @ResponseBody
    public AjaxResult recoverByIds(@PathVariable("ids") List<Integer> ids){
        return toAjax(topCentralControllerAlarmInfoService.recoverBatchByIds(ids));
    }

    /**
     * 删除报警信息
     */
    @RequiresPermissions("central:centralControllerAlarmInfo:remove")
    @Log(title = "报警信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ResponseBody
    public AjaxResult deleteByIds(@PathVariable("ids") List<Integer> ids)
    {
            return toAjax(topCentralControllerAlarmInfoService.logicDeleteBatchByIds(ids));
    }
}
